package com.yh.csx.event.store.gateway.event;

import com.yh.csx.event.store.gateway.event.dataobject.BizEventResultDo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

@Repository
public interface EventResultRepository extends JpaRepository<BizEventResultDo, String> {


    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "replace INTO biz_event_result (id, event_id, group_name, topic, app_name, tag, status, retry_times, ip_address, remark, creator, updater, update_time, result_type) " +
            "VALUES(:#{#resultDo.id}, :#{#resultDo.eventId}, :#{#resultDo.groupName}, :#{#resultDo.topic}, :#{#resultDo.appName}, :#{#resultDo.tag}, :#{#resultDo.status}, 0, :#{#resultDo.ipAddress},:#{#resultDo.remark},:#{#resultDo.creator}, 'sys', :#{#resultDo.updateTime}, 0)")
    void mergeAckEvent(@Param("resultDo") BizEventResultDo resultDo);


    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "insert ignore INTO biz_event_result (id, event_id, group_name, topic, app_name, tag, status, retry_times, ip_address, remark, creator, updater, create_time, update_time, result_type, stack_context,stack_exception) " +
            "VALUES(:#{#resultDo.id}, :#{#resultDo.eventId}, :#{#resultDo.groupName}, :#{#resultDo.topic}, :#{#resultDo.appName}, :#{#resultDo.tag}, :#{#resultDo.status}, 0, :#{#resultDo.ipAddress},:#{#resultDo.remark},:#{#resultDo.creator}, 'sys', now()," +
            " :#{#resultDo.updateTime},1, :#{#resultDo.stackMsg}, :#{#resultDo.stackException})")
    void mergeErrorEvent(@Param("resultDo") BizEventResultDo resultDo);
}
